﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Service;
using LIMS.Entities;
using Castle.Core.Logging;

namespace LIMS.Shared
{
    public class SiteManagementPresenter : BasePresenter<ISiteManagementPageView>
    {
        private ISiteManagementPageView view;
        private ILogger logger;
        private ISiteManagementService SiteService;
        private IPlantManagementService PlantService;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="view"></param>
        /// <param name="cService"></param>
        /// <param name="cUserService"></param>
        /// <param name="oLogger"></param>
        public SiteManagementPresenter(ISiteManagementPageView view, 
                                        ISiteManagementService p_SiteService,
                                       ILogger oLogger,IPlantManagementService PlantService)
            : base(view)
        {
            this.view = view;
            this.SiteService = p_SiteService;
            this.logger = oLogger;
            this.PlantService = PlantService;
        }
        /// <summary>
        /// Initialize View for GroupManagementPresenter
        /// </summary>
        /// <returns></returns>
        public bool InitializeView()
        {
            try
            {
                GridViewData temp = view.GridData;
                temp.Criteria.Clear();
                temp.Criteria.Add(new GVEntityData("Status", CriteriaDataType.String, new List<string> { Resource.ACTIVE_STATUS }, CriteriaSearchType.Equals, CriteriaRelationWithSibling.None));
                GridViewData oGrdData = temp;
                view.Sites = SiteService.GetAllSitesForGrid(ref oGrdData);
                view.GridData = oGrdData;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error while initializing site view. Error : " + ex.Message, ex);
                return false;
            }

        }

        /// <summary>
        /// Saves group
        /// </summary>
        /// <param name="oGroup"></param>
        /// <returns></returns>
        public bool Save(Site oSite)
        {
            return SiteService.Save(oSite);
        }

        /// <summary>
        /// Deletes group
        /// </summary>
        /// <param name="oGroup"></param>
        /// <returns></returns>
        public bool Delete(Site oSite)
        {
            // mark this Site as deactive
            return SiteService.Delete(oSite);
        }
        /// <summary>
        /// Edit Group
        /// </summary>
        /// <param name="index"></param>
        public bool Edit(int index)
        {
            try
            {
                view.CurrentIndex = index;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error occured while editing record. Exception : " + ex.Message, ex);
                return false;
            }
        }



        //public bool IsDuplicateUserName(string sUserName)
        //{
        //    //Check if we have existing user, new user will have Id = 0
        //    if (view.Sites[view.CurrentIndex].Id > 0)
        //    {
        //        //Check if user has changed user name, if not, don't check for duplicate.
        //        if (view.Sites[view.CurrentIndex].Name.ToUpper().Trim() == sUserName.ToUpper().Trim())
        //        {
        //            return false;
        //        }
        //    }
        //    //In rest of the cases perform normal check
        //    if (SiteService .GetAllSites() != null)
        //        return true;
        //    else
        //        return false;
        //}

        /// <summary>
        /// Search User By User Name
        /// </summary>
        /// <param name="sUserName"></param>
        /// <returns></returns>
        public void SearchSiteByKeyword(string sKeyword)
        {
            try
            {
                view.GridData.Criteria.Clear();
                view.GridData.Criteria.Add(new GVEntityData("Name", CriteriaDataType.String, new List<string> { sKeyword }, CriteriaSearchType.Contains, CriteriaRelationWithSibling.Or));
                view.GridData.Criteria.Add(new GVEntityData("CODE", CriteriaDataType.String, new List<string> { sKeyword }, CriteriaSearchType.Contains, CriteriaRelationWithSibling.And));
                view.GridData.Criteria.Add(new GVEntityData("Status", CriteriaDataType.String, new List<string> { Resource.ACTIVE_STATUS }, CriteriaSearchType.Equals, CriteriaRelationWithSibling.None));
                GridViewData oGrdData = view.GridData;
                List<Site> mySites = SiteService.GetAllSitesForGrid(ref oGrdData);
                view.GridData = oGrdData;
                view.Sites = mySites;
            }
            catch (Exception ex)
            {
                logger.Error("Error in Searching user using Keyword for :" + sKeyword, ex);
            }
        }


        /// <summary>
        /// GetPlantBySiteId
        /// </summary>
        /// <param name="SiteID"></param>
        /// <returns></returns>
        public bool GetPlantBySiteId(int SiteID)
        {
            try
            {
                List<Plant> oPlant = new List<Plant>();
                oPlant = PlantService.GetPlantsBySite(SiteID);
                if (oPlant.Count > 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
